package com.thinkgsp.sys.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.thinkgsp.sys.model.SysUser;

public class BaseServiceImpl {
    /**
     * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
     */
    protected final Log log = LogFactory.getLog(getClass());
    
	/**
	 * 获取当前登录的用户
	 * @return
	 */
	public SysUser getCurrentUser() throws AccessDeniedException {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		SysUser currentUser = null;
        if (auth.getPrincipal() instanceof UserDetails) {
            currentUser = (SysUser) auth.getPrincipal();
        } else if (auth.getDetails() instanceof UserDetails) {
            currentUser = (SysUser) auth.getDetails();
        } else {
            throw new AccessDeniedException("您未登陆系统。");
        }
        return currentUser;
	}    
}
